<?php

namespace Modules\Car\Http\Controllers\Api;

use App\Helpers\Constant;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Http\Requests\Order\OrderRequest;

//待支付
class PendingPaymentController extends Controller
{
    use ResponseData,PayTrait;
    public function list(Request $request){
        $user = $request->user();
        $size = $request->input('size',10);
        $data = CarOrder::query()
            ->with('user','customer')
            ->where('department',$user->department)
            ->where('status',Constant::ORDER_CREATE)->paginate($size);
        return $this->success($data);
    }
    public function edit(OrderRequest $request){
        $data = CarOrder::query()
            ->with('user')
            ->where('order_sn',$request->order_sn)
            ->where('status',Constant::ORDER_CREATE)
            ->first();
        return $this->success($data);
    }
    //店长更新金额
    public function update(OrderRequest $request){
        $order = CarOrder::query()
            ->where('status',Constant::ORDER_CREATE)
            ->where('order_sn',$request->order_sn)->first();
        if($order){
            if($request->filled('change_price')){
                if($order->change_price==0){//优惠价
                    if($request->change_price>0){
                        $order->change_price = $request->change_price;
                        // $order->bc_total = $order->bc_total-$request->change_price;
                    }
                }
            }
            if($request->filled('fact_pay')){
                $order->fact_pay = $request->fact_pay;
            }
            if($request->filled('deposit')){
                $order->deposit = $request->deposit;
            }
            if($request->filled('break_rules')){
                $order->break_rules = $request->break_rules;
            }
            if($request->filled('pay_type')){
                $order->pay_type = $request->pay_type;
            }
            $order->status = Constant::ORDER_IS_CONFIRM;
            $order->save();
            return $this->success('更新成功');
        }else{
            return $this->failed('订单不存在',201);
        }
    }
}
